package com.wnn.demo.model.system.controller;

import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Permission;
import com.wnn.demo.domain.Role;
import com.wnn.demo.model.system.service.PermissionService ;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.vo.MenuTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class PermissionController {


    @Autowired
    private PermissionService permissionService;


    /**
     * 系统管理模块-菜单管理，左侧权限树，右侧编辑表框；左侧权限管理树包含信息 菜单+权限
     */
    @GetMapping("/selectMenuAndPermTree")
    public Result selectMenuAndPermTree(){
        List< MenuTreeVo > menuTreeList = permissionService.selectMenuAndPermTree();
        return new Result(ResultCode.OK ,menuTreeList);
    }

    /** 系统管理-权限管理，用于展示表格
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/permission")
    public Result findPermList(String type,@RequestParam(defaultValue = "1")Integer page,
                          @RequestParam(defaultValue = "10")Integer limit){
        PageInfo< Permission > info = permissionService.findPermList(type,page, limit);
        return new Result(ResultCode.OK,info.getTotal(),info.getList());

    }

    /**
     * 根据ID查找权限
     * @param id
     * @return
     */
    @GetMapping("/permission/{id}")
    public Result findPermissionById(@PathVariable(value = "id")String id ){
        Permission permission = permissionService.findById(id);
        return new Result(ResultCode.OK,permission);

    }


    /**
     * 新增权限
     */
    @PostMapping("/permission")
    public Result addPermission(@RequestBody Permission permission){
        HashMap< String, Object > resultMap = permissionService.addPermission(permission);
        int i = (int) resultMap.get("result");
        if (i>0){
            return new Result(ResultCode.OK,resultMap);
        }
        return Result.failure();
    }


    /**
     * 删除一个权限/菜单
     * @param id
     * @return
     */
    @DeleteMapping("/permission/{id}")
    public Result deletePermission(@PathVariable(value = "id")String id){
        int i = permissionService.deletePermissionById(id);
        if (i>0){
            return Result.ok();
        }
        return Result.failure();
    }


    /**
     * 批量删除权限菜单
     * @param ids
     * @return
     */
    @RequestMapping(value="/permissionByBatchDelete",method = RequestMethod.POST)
    public Result batchDeletePerm(String ids) {
        int i = permissionService.batchDeletePerm(ids);
        if (i==0){
            return Result.failure();
        }
        return Result.ok();
    }

    /**
     * 更新一个权限
     * @param id
     * @param map
     * @return
     */
    @PutMapping("/permission/{id}")
    public Result updatePermission(@PathVariable(value = "id")String id,@RequestBody Map<String,Object> map){
        int i = permissionService.updatePermission(id,map);
        if (i>0){
            return Result.ok();
        }
        return Result.failure();
    }

}
